//
//  searchIslanTablview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/29.
//  Copyright © 2016年 game. All rights reserved.
//

#import "searchIslanTablview.h"
#import "IsLanTabCell.h"
#import "IslanListModel.h"
#import "MJRefresh.h"
@interface searchIslanTablview ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSUserDefaults *myud;
@property (nonatomic,strong) IslanListModel *listModel;
@property (nonatomic,strong) FunPagerModel *pageModel;
@property (nonatomic,strong) NSMutableArray *listAry;
@property (nonatomic,assign) NSInteger pageCont;
@end

@implementation searchIslanTablview
-(NSMutableArray *)listAry{
    if (!_listAry) {
        _listAry = [NSMutableArray array];
    }
    return _listAry;
}
-(void)getDateWhithPage:(NSInteger)page{
    
    NSDictionary *tabDic = @{
                               @"latitude":[self.myud valueForKey:@"latitude"],
                               @"longitude":[self.myud valueForKey:@"longitude"],
                               @"page":[NSString stringWithFormat:@"%ld",(long)page],
                               @"name":self.seachString,
                               @"action":@"search",
                               @"city":[self.myud valueForKey:@"cityPinY"],
                               @"page":[NSString stringWithFormat:@"%ld",(long)page],
                               };
    //封装AFNetWorking请求方法
    [[Manager sharedManager]ResponseWhithURL:self.url AndParameter:tabDic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (!error) {//如果请求成功
            
            //数据转化为模型
            self.listModel = [[IslanListModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
            
            self.pageModel = self.listModel.pager;
            
            //将列表数据模型添加到本地数组
            [self.listAry addObjectsFromArray:self.listModel.list];
            self.isShowImage = YES;
            [self reloadData];//刷新表格
            //停止上啦加载刷新
            [self.footer endRefreshing];
            
           

        }else{//如果数据请求失败
            
            //停止上啦加载刷新
            
               self.isShowFile = YES;
            [self reloadData];
            [self.footer endRefreshing];
        }
        
    }];
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    if (self = [super initWithFrame:frame style:style]) {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self registerNib:[UINib nibWithNibName:@"IsLanTabCell" bundle:nil] forCellReuseIdentifier:@"cell"];

        self.separatorStyle = 0;
        self.myud = [NSUserDefaults standardUserDefaults];
        
        
        self.footer  = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh)];
        self.pageCont = 1;
        
        self.footer.automaticallyHidden = YES;
        
        __weak typeof(self) weakSelf = self;
        self.TapBlok = ^{
            
            [weakSelf setSeachString:weakSelf.seachString];
        };

    }

    return self;
}

-(void)setSeachString:(NSString *)seachString{
    _seachString = seachString;
    [self.listAry removeAllObjects];
    [self getDateWhithPage:1];

}

-(void)footRefresh{
    self.pageCont += 1;
    if (self.pageCont>self.pageModel.total_page.integerValue) {
        
        [self.footer noticeNoMoreData];
    }else{
        [self getDateWhithPage:self.pageCont];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listAry.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IsLanTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (self.listAry.count>0) {
        
        cell.model = self.listAry[indexPath.row];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return SCREEN_HEIGHT*0.13;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    IsLanTabCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([self.Maindelget respondsToSelector:@selector(IsLanTabClickWhithDetail:AndLatitude:AndLongitude:AndLocaltion:AndID:AndisYujudian:)]) {
        
         [self.Maindelget IsLanTabClickWhithDetail:cell.detail AndLatitude:cell.latitude AndLongitude:cell.longitude AndLocaltion:cell.location AndID:cell.Id AndisYujudian:self.isYujudian];
    }
   
}

@end